package com.iplant.mes.serviceImpl;

import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.orm.ORMUtils;
import com.iplant.base.utils.sql.orm.PoService;
import com.iplant.basic.service.basicenum.bpm.BPMEventModule;
import com.iplant.basic.service.basicenum.bpm.TaskQueryType;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.basic.utils.Constants;
import com.iplant.mes.service.QMSMaterialCheckService;
import com.iplant.mes.service.mesenum.qms.QMSMaterialCheckStatus;
import com.iplant.mes.service.po.qms.QMSMaterialCheck;
import com.iplant.mes.serviceImpl.dao.qms.QMSMaterialCheckDAO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
public class QMSMaterialCheckServiceImpl implements QMSMaterialCheckService {

    //region 构造函数
    public QMSMaterialCheckServiceImpl() {
        Constants.BPMServiceMap.put(BPMEventModule.MaterialCheck, this);
    }
    //endregion

    //region 单实例

    private static QMSMaterialCheckService Instance = null;

    public static QMSMaterialCheckService getInstance() {
        if (Instance == null)
            Instance = new QMSMaterialCheckServiceImpl();

        return Instance;
    }


    //endregion

    //region POService
    PoService<QMSMaterialCheck> mQMSMaterialCheckService = ORMUtils.GetPoService(QMSMaterialCheck.class);
    //endregion

    //region 流程
    @Override
    public ServiceResult<QMSMaterialCheck> BPM_QueryDefaultTask(BMSEmployee wLoginUser,
                                                                int wTaskID, int wEventID) {
        ServiceResult<QMSMaterialCheck> wResult = new ServiceResult<>();
        wResult.Result = new QMSMaterialCheck();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            Map<String, Object> wParams = new HashMap<String, Object>();
            wParams.put("ID", wTaskID);

            wResult.Result = mQMSMaterialCheckService.SelectPO(wLoginUser, wParams,
                    wErrorCode);

            if (wResult.Result != null && wResult.Result.ID > 0) {
                wResult.Result.FlowType = wEventID;
                wResult.Result.UpFlowID = wLoginUser.ID;
                wResult.Result.UpFlowName = wLoginUser.Name;
            }

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            log.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<QMSMaterialCheck> BPM_CreateTask(BMSEmployee wLoginUser,
                                                          BPMEventModule wEventID) {
        ServiceResult<QMSMaterialCheck> wResult = new ServiceResult<>();
        wResult.Result = new QMSMaterialCheck();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            log.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<QMSMaterialCheck> BPM_SubmitTask(BMSEmployee wLoginUser,
                                                          QMSMaterialCheck wData) {
        ServiceResult<QMSMaterialCheck> wResult = new ServiceResult<>();
        wResult.Result = wData;
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            if (wData.Status == QMSMaterialCheckStatus.Confirm.getValue()) {
                wData.StatusText = QMSMaterialCheckStatus.Confirm.getLabel();
            }

            wData.setUserInfo(wLoginUser);
            wData.EditTime = Calendar.getInstance();
            QMSMaterialCheckDAO.getInstance().UpdatePo(wLoginUser, wData, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            log.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<QMSMaterialCheck> BPM_GetTask(BMSEmployee wLoginUser, int wID) {
        ServiceResult<QMSMaterialCheck> wResult = new ServiceResult<>();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);
            wResult.Result = QMSMaterialCheckDAO.getInstance().SelectPO(wLoginUser, wID, "", wErrorCode);
            wResult.AddFaultCode(wErrorCode);
        } catch (Exception ex) {
            log.error("BPM_GetTask", ex);
        }
        return wResult;
    }

    @Override
    public ServiceResult<List<QMSMaterialCheck>> BPM_QueryEmployeeAllWeb(BMSEmployee wLoginUser,
                                                                         Map<String, Object> wParams,
                                                                         Pagination wPagination) {
        ServiceResult<List<QMSMaterialCheck>> wResult = new ServiceResult<>();
        wResult.Result = new ArrayList<>();
        OutResult<Integer> wErrorCode = new OutResult<>(0);
        try {
            int wStatus = StringUtils.parseInt(wParams.get("Status"));
            switch (wStatus) {
                case 0://待办
                    wParams.remove("CStartTime");
                    wParams.remove("CEndTime");
                    wParams.remove("DStartTime");
                    wParams.remove("DEndTime");
                    wParams.put("Status", "1,2,3,4,5,6");
                    wResult.Result = QMSMaterialCheckDAO.getInstance().SelectPOList(wLoginUser, wParams, wPagination,
                            wErrorCode);
                    break;
                case 1://已办
                    wParams.put("Status", "20,21,22");
                    wResult.Result = QMSMaterialCheckDAO.getInstance().SelectPOList(wLoginUser, wParams, wPagination,
                            wErrorCode);
                    break;
                default://全部
                    wParams.put("Status", "1,2,3,4,5,6,20,21,22");
                    wResult.Result = QMSMaterialCheckDAO.getInstance().SelectPOList(wLoginUser, wParams, wPagination,
                            wErrorCode);
                    break;
            }

            if (wResult.Result.size() == 0 || wStatus == 1) {
                return wResult;
            }

            // 待办数据处理
            List<QMSMaterialCheck> wBaseList =
                    QMSMaterialCheckDAO.getInstance().BFC_GetUndoTaskList(wLoginUser,
                            wLoginUser.ID,
                            new ArrayList<>(Collections.singletonList(BPMEventModule.MaterialCheck.getValue()))
                            , wParams, Pagination.getNewMaxSize(), wErrorCode);
            if (wBaseList == null || wBaseList.size() == 0) {
                return wResult;
            }
            for (QMSMaterialCheck wTaskBase : wBaseList) {
                wTaskBase.TagTypes = TaskQueryType.ToHandle.getValue();
                for (int i = 0; i < wResult.Result.size(); i++) {
                    if (wResult.Result.get(i).ID == wTaskBase.ID)
                        wResult.Result.set(i, wTaskBase);
                }
            }

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            log.error(e.toString());
        }
        return wResult;
    }

    @Override
    public Class<QMSMaterialCheck> GetTClass() {
        return QMSMaterialCheck.class;
    }
    //endregion
}
